/**
 * 微信关注回复
 */

var $table = $('#ProductInfoListTable');

var newAdd = function(id) {

	location.href = ctx + '/weixin/sceneQrcodeEdit';
}

var deleteItem = function(id) {
	if (!confirm('确定要删除吗？')) {
		return;
	}

	$.ajax({
		url : 'deleteSceneQrcode',
		type : 'post',
		data : {
			id : id
		},
		success : function(response) {

			$table.bootstrapTable('refresh');
		},
		error : function(result) {
			_showInfoMessage("操作失败。", 'error');
		}
	});
}
var config = {
	".chosen-select" : {},
	".chosen-select-deselect" : {
		allow_single_deselect : !0
	},
	".chosen-select-no-single" : {
		disable_search_threshold : 10
	},
	".chosen-select-no-results" : {
		no_results_text : "Oops, nothing found!"
	},
	".chosen-select-width" : {
		width : "95%"
	}
};

$(document)
		.ready(
				function() {

					// 将表单元素和table属性转成json
					queryParams = function(params) {
						var temp = {};
						temp.currentResult = params.offset;
						temp.showCount = params.limit;
						var formParam = $('#queryForm').serializeArray();
						for (var i = 0; i < formParam.length; i++) {
							if (formParam[i].value != '') {
								temp[formParam[i].name] = formParam[i].value;
							}
						}
						return temp;
					};

					detailFormatter = function(index, row) {
						var html = [];
						if (row.replyType) {
							html.push('<p><b>用户名:</b> ' + row.replyType
									+ '</p>');
						}

						return html.join('');
					}

					rowStyle = function(row, index) {
						var classes = [ 'active', 'success', 'info', 'warning',
								'danger' ];
						if (index % 2 === 0 && index / 2 < classes.length) {
							return {
								classes : classes[index / 2]
							};
						}
						return {};
					}

					$table
							.bootstrapTable({
								url : ctx + "/weixin/getSceneQrcode",
								pagination : !0,
								showRefresh : !0,
								showToggle : !0,
								showColumns : !0,
								iconSize : "outline",
								striped : true, // 是否显示行间隔色
								queryParams : queryParams,// 传递参数（*）
								sidePagination : "server", // 服务端处理分页
								pageSize : 10, // 每页的记录行数（*）
								pageList : [ 5, 10, 20, 50 ], // 可供选择的每页的行数（*）
								minimunCountColumns : 2, // 可供选择的每页的行数（*）
								// detailView: true,
								sortName : undefined,
								sortOrder : 'asc',
								sortStable : false,
								type : "post",
								toolbar : "#tableEventsToolbar",
								contentType : "application/json; charset=utf-8",
								// detailFormatter : detailFormatter,
								// rowStyle : rowStyle,
								iconsPrefix : 'glyphicon', // glyphicon of fa
								// (font awesome)
								icons : {
									refresh : "glyphicon-repeat",
									toggle : "glyphicon-list-alt",
									columns : "glyphicon-list",
									detailOpen : 'glyphicon-plus icon-plus',
									detailClose : 'glyphicon-minus icon-minus',
									sortable : 'glyphicon-minus icon-minus'
								},
								onLoadSuccess : function(data) {
									if (data.total < 5) {
										$table.bootstrapTable('getOptions')["height"] = 400;
									} else {
										$table.bootstrapTable('getOptions')["height"] = '';
									}

									$table.bootstrapTable('hideColumn', 'id');
									$table.bootstrapTable('hideColumn', 'edit');
								},
								columns : [
										{
											title : '名 称',
											field : 'name',
											align : 'center',
											valign : 'middle'
										},
										{
											title : '二维码类型',
											field : 'limitType',
											align : 'center',
											valign : 'middle',
											formatter : function(value, row,
													index) {
												if (value == '0') {
													return '临时二维码';
												} else if (value == '1') {
													return '永久二维码';
												}
												return value;
											}
										},
										{
											title : '过期时间',
											field : 'expiredTime',
											align : 'center',
											valign : 'middle'
										},
										{
											title : '二维码图片',
											field : 'qrcodeUrl',
											align : 'center',
											valign : 'middle',
											formatter : function(value, row,
													index) {
												return '<img src="'
														+ value
														+ '" style="width:100px;" />';
											}
										},
										{
											title : '创建时间',
											field : 'createdTime',
											align : 'center',
											valign : 'middle'
										},
										{
											title : '操作',
											field : 'operate',
											align : 'center',
											width : '140',
											valign : 'middle',
											formatter : function(value, row,
													index) {
												return '<a href="sceneQrcodeEdit?id='
														+ row.id
														+ '"><button class="btn  btn-xs"  title="修改"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></button></a>'
														+ '&nbsp;<a onclick="deleteItem('
														+ row.id
														+ ')"><button class="btn  btn-xs"  title="删除"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></a>'
														+ '&nbsp;<a href="sceneQrcodeStatis?id='
														+ row.id
														+ '"><button class="btn  btn-xs" title="统计"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span></button></a>';

											}
										/*
										 * }, { title : '操作', field : 'edit',
										 * align : 'center', width : '200',
										 * valign : 'middle'
										 */
										} ]
							});
					for ( var selector in config)
						$(selector).chosen(config[selector]);
				});